#variable definitions
COMPILER       := cc
SOURCES_TST    := TheTest.c
OBJECTS_TST    := $(SOURCES_TST:.c=.o)
OUTPUT         := TheTest.bin
KERNEL_DIR     := /lib/modules/$(shell uname -r)/build
PWD            := $(shell pwd)
SOURCES_DRIVER := TheBuffer.o RingBuffCore.o
OBJECTS_DRIVER := $(SOURCES_DRIVER:.c=.o)


#make directive for Kernel driver object
obj-m        := $(SOURCES_DRIVER)

#Build directives
TheBuffer:
	@echo "Building the ELE784 Ring Buffer driver (by Vince and Jesus)..."
	@echo "The Buffer"
	@echo "Now building..."
	@make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
	@echo "Build finished... TheBuffer ready to be installed"
	@echo "\nSVP, écrire 'sudo insmod ./TheBuffer.ko' pour installer le pilote"
	@echo "(Retirer du système avec 'sudo rmmod TheBuffer')\n"

TheTest:
	@echo "Now building The Test "
	@echo "Compiling..."
	@$(COMPILER) -c $(SOURCES_TST)
	@echo "Linking..."
	@$(COMPILER) -o $(OUTPUT) $(OBJECTS_TST)
	@echo "Build finished... Generated file: $(OUTPUT)\n" 
	@echo "SVP, écrire './$(OUTPUT)' pour démarrer le test !\n"

all: 	
	@clear
	@echo "BUILDING ALL..."
	@make TheBuffer
	@make TheTest

#Clean up module
clean:
	@-rm -f *.bin *.o *.ko .*.cmd .*.flags *.mod.c Module.symvers modules.order
	@-rm -rf .tmp_versions
	@echo "Cleaned up all objects and outputs from workspace"

